#include <iostream>
#include <thread>

#include "reactor.h"
#include "event.h"
#include "block_queue.h"
#include "socket_base.h"

void client(SocketReactor* sr)
{
    Socket* sk = SocketMGR::NewConnect("127.0.0.1", 8888);
    Reader* rd = new Reader(sr, sk);
    sr->Regist(rd);

    do {
        std::cout << "IN>>";
        std::string d;
        std::cin >> d;
        sk->Send(PackBuffer(d.data(), d.length()));
        auto q = QueueRepo::Repo()->Queue<SharedBuffer>("SN");
        auto msg = q->Pop();
        std::cout << "MSG:" << msg->Get()<< std::endl;
    } while(true);
}

int main()
{
    SocketMGR* mgr = new SocketMGR();
    SocketReactor* sr = new SocketReactor(mgr);
    Acceptor* a = new Acceptor(sr, 8888, "SN");
    Thread* thr = new Thread(sr);

    sr->Regist(a);

    std::thread thrd(&client, sr);

    thr->wait();
    thrd.join();

    return 0;
}
